<?php
/**
 * Created by PhpStorm.
 * User: Liuhongchuan
 * Date: 2018/4/24
 * Time: 10:30
 */

namespace app\smallapp\controller;
use app\common\controller\BaseApi;
use think\Db;

class Treadmill extends BaseApi {
    public function _initialize()
    {
//        parent::_initialize();
//        if (!request()->isPost()) {
//            return $this->api_ajax_error("","非法访问");
//        }
        $time = input("time/d","");
        $sign = input("sign/s","");
        if (input("debug") !=1 ) {
            if (($time + 300) < time() || ($time - 300) > time()) {//接口调用时间操过5分钟
                return $this->api_ajax_error("","链接已经过期");
            }
            if ($sign != md5("ocmcom_me_" . $time)) {
                return $this->api_ajax_error("","签名错误！");
            }
        }
    }

    /**
     * 获取用户的跑步机数据
     */
    public function getTreadmillRecord() {

        $member_id = input("member_id/d");
        $shop_id = input("shop_id/d");
        $start_date = input("start_date/s");
        $end_date = input("end_date/s");

        if (empty($start_date) || empty($end_date)) {
            $start_date = $end_date = date("Y-m-d");
        }
        if (empty($member_id) || empty($shop_id)) {
            return $this->api_ajax_error("","参数错误");
        }else{
            $where["shop_id"] = ['eq', $shop_id];
            $where["gym_member_id"] = ['eq', $member_id];
            $where["is_delete"] = 0;
            $cards = db("gym_order")->where($where)->column("card_no");
            if (empty($cards)) {
                return $this->api_ajax_error("","无效的member_id");
            }
            $cards_str = implode(",", $cards);
            $map["create_time"] = ['between time', [$start_date . " 00:00:00", $end_date . " 23:59:59"]];
            $map["card_no"] = ["in", $cards];
            $has_record = db("sport_treadmill")->where($map)->count();
            if (empty($has_record)) {
                $this->api_ajax_success(array(), "无跑步记录");
            }
            if ($start_date == $end_date) {
                $field = "convert(sum(run_distance),decimal(16,2))  as run_distance, convert(max(avg_speed),decimal(15,2)) as avg_speed, convert(max(avg_incline),decimal) as avg_incline, sum(burn_cal) as burn_cal, convert(max(heart_rate), decimal) as heart_rate, sum(sport_time) as sport_time, sum(pace) as pace";
            } else {
                $field = "convert(sum(run_distance),decimal(16,2))  as run_distance,  sum(burn_cal) as burn_cal,  sum(sport_time) as sport_time, sum(pace) as pace";
            }
            $sport_record = db("sport_treadmill")->where($map)
                ->field($field)
                ->find();

            if ($start_date == "1970-01-01") { //计算总运动天数
                $continue_days = Db::query("SELECT * FROM (SELECT * FROM ( SELECT shop_id, max(days) continue_days, min(login_day) start_date, max(login_day) end_date FROM (SELECT shop_id, @cont_day := (CASE WHEN (@last_shop_id = shop_id AND DATEDIFF(login_dt, @last_dt) = 1) THEN (@cont_day + 1) WHEN (@last_shop_id = shop_id AND DATEDIFF(login_dt, @last_dt) < 1) THEN (@cont_day + 0) ELSE 1 END) AS days, (@cont_ix := (@cont_ix + IF(@cont_day = 1, 1, 0))) AS cont_ix, @last_shop_id := shop_id, @last_dt := login_dt login_day FROM (SELECT shop_id, DATE(create_time) login_dt FROM m_sport_treadmill WHERE shop_id = {$shop_id} and card_no in ({$cards_str}) ORDER BY shop_id, create_time) AS t, (SELECT @last_shop_id := '', @last_dt := '', @cont_ix := 0, @cont_day := 0) AS t1 ) AS t2 GROUP BY shop_id, cont_ix ) tmp ORDER BY continue_days DESC) ntmp GROUP BY shop_id;");
                $sport_record["continue_days"] = $continue_days[0]['continue_days'];
            }
            return $this->api_ajax_success($sport_record);
        }

//        if (empty($member_id) || empty($shop_id)) {
//            return $this->api_ajax_error("","参数错误");
//        }else{
//            $where["shop_id"] = ['eq', $shop_id];
//            $where["gym_member_id"] = ['eq', $member_id];
//            $cards = db("gym_order")->where($where)->column("card_no");
//            if (empty($cards)) {
//                return $this->api_ajax_error("","无效的member_id");
//            }
//            $map["create_time"] = ['between time', [$sport_date . " 00:00:00", $sport_date . " 23:59:59"]];
//            $map["card_no"] = ["in", $cards];
//            $has_record = db("sport_treadmill")->where($map)->count();
//            if (empty($has_record)) {
//                $this->api_ajax_success(array(), "今日无跑步记录");
//            }
//            $sport_record = db("sport_treadmill")->where($map)
//                ->field("convert(sum(run_distance),decimal(16,2))  as run_distance, convert(avg(avg_speed),decimal(15,2)) as avg_speed, convert(avg(avg_incline),decimal) as avg_incline, sum(burn_cal) as burn_cal, convert(avg(heart_rate), decimal) as heart_rate, sum(sport_time) as sport_time, sum(pace) as pace")
//                ->find();
//            return $this->api_ajax_success($sport_record);
//        }
    }
}